﻿using System;
using System.Linq;
using System.Web.Services;
using Dost.Khcn.WebOnline.Data;
using Dost.Khcn.WebOnline.Models;

namespace Dost.Khcn.WebOnline
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod(EnableSession = true)]
        public static object LinhVucs()
        {
            using (var db = new NlkhOnlineEntities())
            {
                var linhVucs = (from lv in db.LinhVucs
                                select new { lv.Id, lv.Ten }
                                ).OrderBy(lv => lv.Ten).ToList();

                linhVucs.Insert(0, new { Id = 0, Ten = string.Empty });
                return linhVucs;
            }
        }

        [WebMethod(EnableSession = true)]
        public static object HocHams()
        {
            //System.Threading.Thread.Sleep(2000);
            using (var db = new NlkhOnlineEntities())
            {
                //var linhVucs = db.LinhVucs.ToList();
                var hocHams = (from hh in db.HocHams
                               select new { hh.Id, hh.Ten }
                                ).ToList();

                hocHams.Insert(0, new { Id = 0, Ten = string.Empty });
                return hocHams;
            }
        }

        [WebMethod(EnableSession = true)]
        public static object HocVis()
        {
            using (var db = new NlkhOnlineEntities())
            {
                var hocVis = (from hv in db.HocVis
                              select new { hv.Id, hv.Ten }
                    ).OrderBy(hv => hv.Ten).ToList();

                hocVis.Insert(0, new { Id = 0, Ten = string.Empty });
                return hocVis;
            }
        }

        [WebMethod(EnableSession = true)]
        public static object SearchChuyenGia(int jtStartIndex, int jtPageSize, string jtSorting, SearchChuyenGiaModel searchModel)
        {
            if (jtPageSize > 25) jtPageSize = 50;
            using (var db = new NlkhOnlineEntities())
            {
                var totalRecords = db.SearchChuyenGiaOnlineCount(searchModel.HoTen, searchModel.TenDonVi, searchModel.TenCongTrinh,
                    searchModel.LinhVucId, searchModel.HocHamId,
                    searchModel.HocViId).FirstOrDefault().Value;

                var results = db.SearchChuyenGiaOnline(jtStartIndex, jtPageSize, jtSorting, searchModel.HoTen, searchModel.TenDonVi, searchModel.TenCongTrinh,
                    searchModel.LinhVucId, searchModel.HocHamId,
                    searchModel.HocViId).ToList();

                return new { Result = "OK", Records = results, TotalRecordCount = totalRecords };
            }
        }
    }
}